//#############################################################################
//#############################################################################
//This source file was written by Mert Dogar. If you have some questions about 
//the source code, please do not hesitate to ask me.
//The code is under GPL.
//Email: mertdogar@gmail.com , http://code.google.com/p/electron-cds
//#############################################################################
//#############################################################################

#include "sheet.h"

std::string sheet::find_new_name(std::string prefix)
{
	std::string tmp;
	tmp=prefix;
	
	
	for(int i = 0;i < components.size(); i++)
	{	
		//log("asf");
		if(components[i].name==tmp)
		{
			
			tmp==prefix + (char)i;	
			//log(tmp);
			
		}
	}
	return tmp;
}

void sheet::tadd_component(Cairo::RefPtr<Cairo::Context> cr,std::string strname, vector2 pos)
{
	pos.x =  pos.x / factor + sroot.x;
	pos.y=  pos.y / factor + sroot.y;
	std::string prefix = "./library/";
	prefix += strname;

	cbase tmp(prefix,find_new_name (strname),pos);
	
	components.push_back(tmp);
	print_components (cr);
	components.pop_back();
}

cbase &sheet::get_component(int index)
{
	
	if(index>=0 && index < components.size())
		return components[index];
	
	
	
}
void sheet::add_wire(vector2 start, vector2 end, float line_width)
{
	start.x =  start.x / factor + sroot.x;
	start.y=  start.y / factor + sroot.y;
	end.x =  end.x / factor + sroot.x;
	end.y=  end.y / factor + sroot.y;
	cbase tmp(start,end,line_width,torgbcolor (0,1,0));

	components.push_back(tmp);

}

void sheet::add_component(std::string strname, vector2 pos)
{
	pos.x =  pos.x / factor + sroot.x;
	pos.y=  pos.y / factor + sroot.y;
	std::string prefix = "./library/";
	prefix += strname;

	cbase tmp(prefix,find_new_name (strname),pos);

	components.push_back(tmp);
	
}
void sheet::setroot(vector2 vec)
{
	sroot = tovector2 (vec.x,vec.y);
}
void sheet::print_components(Cairo::RefPtr<Cairo::Context> cr)
{	
	if(!changed)
		return;
	
	clear_screen(cr);
	//dell(cr);
	for (unsigned int i = 0; i<components.size();i++)
	{

		components[i].draw(cr,torgbcolor (-1,-1,-1),factor,sroot);
	}
	
	changed=false;
}
void sheet::make_null_selected(Cairo::RefPtr<Cairo::Context> cr, int index)
{
	components[index].draw_area(cr,torgbcolor (0,0,0),factor,sroot);
		
}
void sheet::clear_screen(Cairo::RefPtr<Cairo::Context> cr)
{
	cr->set_source_rgba(0,0,0,1);
	cr->paint();
}	
int sheet::isonwho(Cairo::RefPtr<Cairo::Context> cr, vector2 pos)
{	int ptr=-1;
	
	
		clear_screen(cr);
	//dell(cr);
	pos.x = pos.x / factor  + sroot.x;
	pos.y = pos.y / factor  + sroot.y;
	

	for (int i = 0; i<components.size();i++)
	{		
		if(components[i].select(cr,pos,torgbcolor(1,0.5,0),factor,sroot,true)==true)
			ptr=i;
	}
	return ptr;
	
	
}
void sheet::slect_by_index(int index)
{
	components[index].select(cr,pos,torgbcolor(1,0.5,0),factor,sroot,true);
}
void sheet::dell(Cairo::RefPtr<Cairo::Context> cr)
{	
	


	for (int i = 0; i<components.size();i++)
	{		
		components[i].draw(cr,torgbcolor (1,1,1),factor,sroot);
		std::cout << "d" << i << std::endl;
	}

}
